package me.ak.gui;

import me.ak.model.ScoreModel;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.InputAdapter;

import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.GUI;
import de.matthiasmann.twl.Label;
import de.matthiasmann.twl.Widget;
import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
import de.matthiasmann.twl.theme.ThemeManager;

public class MainCanvas extends BasicGameState {
	Button btn;

	public GUI gui;
	public static Widget root;
	public SpriteSheet hands;
	public GuiScore parent;
	
	public MainCanvas(GuiScore m) {
		parent = m;
	}

	@Override
	public void init(GameContainer gc, StateBasedGame game) throws SlickException {
		root = new Widget();
		root.setTheme("");

		GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
		try {
			LWJGLRenderer renderer = new LWJGLRenderer();
			ThemeManager theme = ThemeManager.createThemeManager(Canvas.class.getResource("/me/ak/utils/chutzpah.xml"), renderer);
			gui = new GUI(root, renderer);
			gui.applyTheme(theme);
		} catch (Exception e) {
		} finally {
			GL11.glPopAttrib();
		}

		TWLInputForwarder tia = new TWLInputForwarder(gui, gc.getInput());
		gc.getInput().addPrimaryListener(new InputForwarder());
		gc.getInput().addPrimaryListener(tia);

		Label toolbarLabel = new Label("Toolbar");
		Button button1 = new Button("Click");
		Button button2 = new Button("Clickme");
		ToolbarWidget tb = new ToolbarWidget();
		Button b = new Button("yo");
		b.addCallback(new Runnable(){
			public void run() {
				Alert.displayMessage("Warning... There is a bus you are about to get hit by.");
			}
		});
		tb.addWidgets(toolbarLabel, button1, button2, b);
		tb.setType(ToolbarWidget.Type.VERTICAL);
		root.add(tb);
		
		BarRenderer.updateScore((ScoreModel) parent.getModel());
	}


	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		//g.scale(z, z);
		g.translate(transx, transy);
		g.setColor(Color.white);
		g.setBackground(Color.black);
		// Canvas.theScore.draw(g);
		
		for (BarRenderer.GuiBar b : BarRenderer.bars){
			b.draw(g);
		}
		
		gui.draw();
	}

	public float transx = 0, transy = 0;
	public static float z = 1;

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int arg2) throws SlickException {
		gui.update();
	}

	@Override
	public int getID() {
		return 0;
	}

	public boolean isPointInGui(int x, int y) {
		return gui.getWidgetAt(x, y) != null && gui.getWidgetAt(x, y) != root;
	}

	public boolean isPointInNote(int x, int y) {
		for (BarRenderer.GuiBar b : BarRenderer.bars) {
			if (b.bounds.contains(x, y)) {
				return true;
			}
		}
		return false;
	}

	public Widget getToolbarAtCursor(int x, int y) {
		Widget w = gui.getWidgetAt(x, y);
		if (w == null || w == root) {
			return null;
		}

		if (w instanceof ToolbarWidget) {
			return w;
		}
		return null;
	}

	public static boolean canDragScore = false;

	private class InputForwarder extends InputAdapter {
		@Override
		public void mouseDragged(int x, int y, int x1, int y1) {
			if (Alert.stopInteract) return;
			if (!isPointInGui(x, y) && canDragScore) {
				transx += x1 - x;
				transy += y1 - y;
			} else if (isPointInGui(x, y)) {
				canDragScore = false;
			}
		}

		@Override
		public void mousePressed(int button, int x, int y) {
			if (Alert.stopInteract) return;
			if (!isPointInGui(x, y)) {
				canDragScore = true;
			}
		}

		@Override
		public void mouseReleased(int button, int x, int y) {
			if (Alert.stopInteract) return;
			canDragScore = false;
		}

		@Override
		public void mouseWheelMoved(int change) {
			if (Alert.stopInteract) return;
			int mouseY = Canvas.HEIGHT - Mouse.getY();
			if (isPointInGui(Mouse.getX(), mouseY)) return;
			z += (change/120)*0.14;
			transx -= 5f;
			transy -= 5f;
			if (z < 0.5) z = 0.5f;
			if (z > 3) z = 3f;
		}
	}

}
